/**
 * Created by HUANGFR2 on 8/24/2016.
 */

app = angular.module('clientApp', ['ngRoute','ordersApp', 'dishApp','merchants', 'ui.bootstrap']);

app.service('merchantService', function ($http, $rootScope) {
    var service = {
        merchantInfo: function (cb) {
            var req = {
                method: "GET",
                url: "merchants/loadMerchant"
            };
            $http(req)
                .then(function (res) {
                    var type = null;
                    switch (res.data.type) {
                        case 1:
                            type = 'Fast Food'
                            break;
                        case 2:
                            type = 'Special Food'
                            break;
                        case 3:
                            type = 'Western Food'
                            break;
                        case 4:
                            type = 'Refreshment'
                            break;
                        case 5:
                            type = 'Dessert'
                            break;
                        case 6:
                            type = 'Fruit'
                            break;
                    }
                    res.data.types = type
                    $rootScope.merchant = res.data
                    if(cb){
                        cb(res.data);
                    }
                });
        }
    };
    return service;
});

app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            redirectTo: '/home'
        })
        .when('/home', {
            templateUrl: '/merchant/views/merchant.client.view.html'
        })
        .when('/updateInfo', {
            templateUrl: '/merchant/views/merchant.client.update.html'
        }).when('/dishList', {
        templateUrl: '/dish/views/dish.client.list.html'
    }).when('/newOrders', {
        templateUrl: '/order/views/order.client.new.html'
    }).when('/addDish', {
        templateUrl: '/dish/views/dish.client.add.html'
    }).when('/ordersList', {
        templateUrl: '/order/views/order.client.list.html'
    }).when('/myComplain', {
        templateUrl: '/merchant/views/merchant.client.complaint.html'
    }).when('/orderDetails', {
        templateUrl: '/order/views/order.client.detail.html'
    })

});

app.run(function ($rootScope, $location, $timeout, $http,merchantService) {
    $rootScope.$on('$routeChangeStart', function () {
        var url = $location.url();
        $rootScope.permission = true;
            if (url == '/myComplain' || url == '/ordersList' || url == '/newOrders' || url == '/dishList' || url == '/home' || url == '/updateInfo') {

                merchantService.merchantInfo(function(data){
                    if (data) {
                        $rootScope.merchant = data;
                        $rootScope.permission = true;
                /*
                var req = {
                    method: "GET",
                    url: "merchants/loadMerchant"
                };
                $http(req).then(function (res) {
                    var exist = res.data;
                    if (exist) {
                        var type = null;
                        switch (exist.type) {
                            case 1:
                                type = 'Fast Food'
                                break;
                            case 2:
                                type = 'Special Food'
                                break;
                            case 3:
                                type = 'Western Food'
                                break;
                            case 4:
                                type = 'Refreshment'
                                break;
                            case 5:
                                type = 'Dessert'
                                break;
                            case 6:
                                type = 'Fruit'
                                break;
                        }
                        exist.types = type
                        $rootScope.merchant = exist
                        $rootScope.permission = true;*/
                    } else {
                        $rootScope.permission = false;
                    }
                },function (res) {

                })

            }
    });

});

app.service('constant', function () {
    var service = {
        imgServer:'http://zha-ita113-w7:8089/'
    };
    return service;
});

app.controller("logoutController",function ($http,$scope,$rootScope,constant) {

    $rootScope.constant = constant;
    $scope.logout = function(){
        var req = {
            method: "GET",
            url: "merchants/logout"
        };
        $http(req)
            .then(function (res) {
                if(res.data.msg == 'SUCCESS'){
                    window.location = 'login.html'
                }else{

                }
            });
    }
});

app.service('newOrders', function ($http, $rootScope) {
    var service = {
        newOrders: function(){
            setInterval(function () {
                var req = {
                    method: "GET",
                    url: "orders/newOrders/size"
                };
                $http(req)
                    .then(function (res) {
                        if(res.data.length!=0){
                            $rootScope.new = res.data.length;
                            $rootScope.flag = true;
                        }else{
                            $rootScope.flag = false;
                            $rootScope.new = '';
                        }
                    });
            },10000)
        }
    };
    return service;
});
